﻿using System.Collections.Generic;

namespace LeastSquareMethod
{
    /// <summary>
    /// 数据类
    /// </summary>
    public class Data
    {
        public int year { get; set; }
        public double GDP { get; set; }
    }


    internal class Program
    {



        //将数据存入链表里
        static void DataList(List<Data> data)
        {
            var data1 = new Data()
            {
                year = 2006,
                GDP = 27521.32
            };
            var data2 = new Data()
            {
                year = 2007,
                GDP = 35503.43
            };
            var data3 = new Data()
            {
                year = 2008,
                GDP = 45943.07
            };
            var data4 = new Data()
            {
                year = 2009,
                GDP = 51017.03
            };
            var data5 = new Data()
            {
                year = 2010,
                GDP = 60871.64
            };
            var data6 = new Data()
            {
                year = 2011,
                GDP = 75515.00
            };
            var data7 = new Data()
            {
                year = 2012,
                GDP = 85322.30
            };
            var data8 = new Data()
            {
                year = 2013,
                GDP = 95704.06
            };
            var data9 = new Data()
            {
                year = 2014,
                GDP = 104756.83
            };
            var data10 = new Data()
            {
                year = 2015,
                GDP = 110615.53
            };
            var data11 = new Data()
            {
                year = 2016,
                GDP = 112332.77
            };
            var data12 = new Data()
            {
                year = 2017,
                GDP = 123104.09
            };
            var data13 = new Data()
            {
                year = 2018,
                GDP = 138948.18
            };
            var data14 = new Data()
            {
                year = 2019,
                GDP = 142799.38
            };
            var data15 = new Data()
            {
                year = 2020,
                GDP = 146876.74
            };
            data.Add(data1);
            data.Add(data2);
            data.Add(data3);
            data.Add(data4);
            data.Add(data5);
            data.Add(data6);
            data.Add(data7);
            data.Add(data8);
            data.Add(data9);
            data.Add(data10);
            data.Add(data11);
            data.Add(data12);
            data.Add(data13);
            data.Add(data14);
            data.Add(data15);
        }

        static public double YearAverage, GDPAverage;
        static public double b, a;
        static public double temp1, temp2;

        static public void GetAverage(List<Data> data)
        {
            double YearSum = 0, GDPSum = 0;
            for (int i = 0; i < data.Count; i++)
            {
                YearSum += data[i].year;
                GDPSum += data[i].GDP;
            }
            YearAverage = YearSum / data.Count;
            GDPAverage = GDPSum / data.Count;
        }

        static public void Get_b(List<Data> data)
        {
            double temp1 = 0;
            double temp2 = 0;
            for (int i = 0; i < data.Count; i++)
            {
                temp1 += (data[i].year - YearAverage) * (data[i].GDP - GDPAverage);
                temp2 += (data[i].year - YearAverage) * (data[i].year - YearAverage);

            }
            b = temp1 / temp2;
        }


        static public void Get_a(List<Data> data)
        {
            a = GDPAverage - b * YearAverage;
        }

        static void Main(string[] args)
        {
            List<Data> data = new List<Data>();
            DataList(data);
            GetAverage(data);
            Get_b(data);
            Get_a(data);
            double data16 = b * 2021 + a;
            Console.WriteLine($"2021年预测值为{data16}");
            Console.WriteLine("2021年真实值为177340.63");
            double difference = data16 - 177340.63;
            Console.WriteLine($"偏差为{difference / 177340.63*100}%");
        }


    }




}